package com.ifd.as400;

import org.apache.log4j.Logger;

import com.ibm.as400.access.AS400;
import com.ifd.controller.SplfDefaultProperties;


public class IFDConnectionPool{
	static final Logger logger = Logger.getLogger(IFDConnectionPool.class);
	private static IFDConnectionPool instance = null;
	public static IFDConnectionPool init(){
		if(instance == null)
			instance = new IFDConnectionPool();
		return instance;
	}
	public static void closePool(){
		if(instance!=null)
			instance.closeConnection();
		logger.info("The connection is closed");
	}
	public static AS400 getAS400(){
		if(instance==null)
			init();
		return instance.getConnection();
	}
	
	private AS400 as400=null;
	private IFDConnectionPool(){
		String system;
		try {
			SplfDefaultProperties conf = new SplfDefaultProperties();
			system=conf.getProperty("SYSTEM");
			as400 = new AS400(system);
		} catch (Exception e) {
			as400 = new AS400();
		}
	}
		
	private AS400 getConnection(){
		try {
			if(!as400.isConnected())
				as400.connectService(AS400.COMMAND);
		} catch (Exception e) {
		}
		return as400; 
	}
	
	private void closeConnection(){
		if(as400.isConnected())
			as400.disconnectAllServices();
	}
}